package cn.jinbyte.data.service;

import org.springframework.data.redis.core.RedisTemplate;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * Redis缓存服务接口
 *
 * @author jinty
 */
public class RedisCacheService<V> implements CacheService<V> {

    private final RedisTemplate<String, V> redisTemplate;

    public RedisCacheService(RedisTemplate<String, V> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void set(String key, V value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void set(String key, V value, long timeout, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    @Override
    public void expire(String key, final Duration timeout) {
        redisTemplate.expire(key, timeout);
    }


    @Override
    public V get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public void del(String key) {
        redisTemplate.delete(key);
    }
}
